<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-8-4
 * Time: 下午8:55
 */

namespace common\members\wodrow\components\yii2app;


use common\config\Env;
use common\models\db\Yii2App;
use yii\base\Component;
use yii\base\Exception;

/**
 * Class Appinfo
 * @package common\components\wodrow\yii2app
 *
 * @property Yii2App $yii2App
 * @property string $homeUrl
 * @property string $name
 */
class Appinfo extends Component
{
    public $name;

    /**
     * @var Yii2App $yii2_app
     */
    private $_yii2_app;

    public function init()
    {
        parent::init();
        $this->_yii2_app = Yii2App::findOne(['name' => $this->name]);
    }

    public function getYii2App()
    {
        return $this->_yii2_app;
    }

    /**
     * @return string $homeUrl
     * @throws Exception
     */
    public function getHomeUrl()
    {
        switch ($this->name){
            case Env::$yii2appid_home:
                $url = Env::getUrls()['home_url'];
                break;
            case Env::$yii2appid_admin:
                $url = Env::getUrls()['admin_url'];
                break;
            case Env::$yii2appid_appapi:
                $url = Env::getUrls()['appapi_url'];
                break;
            case Env::$yii2appid_console:
                throw new Exception("console应用没有homeUrl");
                break;
            default:
                throw new Exception("没有找到应用");
                break;
        }
        return $url;
    }
}